﻿using Magicodes.ExporterAndImporter.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NP.BPMReportPlatform
{
    /// <summary>
    /// Excel导入结果
    /// </summary>
    public class ImportResultExt<T> where T : class
    {
        /// <summary>
        /// </summary>
        public ImportResultExt()
        {
            RowErrors = new List<DataRowErrorInfo>();
        }

        /// <summary>
        ///     导入数据
        /// </summary>
        public virtual ICollection<T> Data { get; set; }

        /// <summary>
        ///     验证错误
        /// </summary>
        public virtual IList<DataRowErrorInfo> RowErrors { get; set; }

        /// <summary>
        ///     模板错误
        /// </summary>
        public virtual IList<TemplateErrorInfo> TemplateErrors { get; set; }

        /// <summary>
        ///     导入异常信息
        /// </summary>
        public virtual Exception Exception { get; set; }

        /// <summary>
        ///     是否存在导入错误
        /// </summary>
        public virtual bool HasError => Exception != null ||
                                        (TemplateErrors?.Count(p => p.ErrorLevel == ErrorLevels.Error) ?? 0) > 0 ||
                                        (RowErrors?.Count ?? 0) > 0;

        /// <summary>
        ///自定义异常信息
        /// </summary>
        public virtual string ExceptionMessage => HasError ? Exception != null ? Exception.Message : (TemplateErrors?.Count > 0 ? TemplateErrors[0].Message : $"第{RowErrors[0].RowIndex}行{RowErrors[0].FieldErrors.Values.ElementAt(0)}") : "";
    }
}
